<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>

<body>
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="hidden" name="id" value="" lay-verify="required" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" lay-verify="required" autocomplete="off"
                                placeholder="角色名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色描述</label>
                        <div class="layui-input-block">
                            <input type="text" name="remark" value="" autocomplete="off" placeholder="请输入角色描述"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <input type="radio" name="state" value="1" title="启用" checked>
                            <input type="radio" name="state" value="0" title="禁用">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">拥有的权限</label>
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                            <div id="test12" class="demo-tree-more"></div>
                        </fieldset>

                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script src="../../../component/layui/layui.js"></script>
    <script src="../../../component/pear/pear.js"></script>

    <script>
        layui.use(['form', 'jquery', 'tree', 'util'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            var tree = layui.tree
            var layer = layui.layer;
            var util = layui.util;
            // 从 URL 中获取 id 参数的值
            function getUrlParam(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
            // 获取 id 值
            var id = getUrlParam('id');
            // 页面加载时向后端发送请求获取需要回显的数据
            $.ajax({
                url: 'http://localhost:8080/role/query?id=' + id,
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('token', localStorage.getItem("token"));
                },
                success: function (res) {
                    $("input[name='id']").val(res.data[0].id);
                    $("input[name='name']").val(res.data[0].name);
                    $("input[name='remark']").val(res.data[0].remark);
                    $("input[name='state'][value='" + res.data[0].state + "']").prop('checked', true);
                }
            });

            $.ajax({
                url: 'http://localhost:8080//menu/queryDynamicMenu?userId=' + id,
                type: 'get',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('token', localStorage.getItem("token"));
                },
                success: function (res) {
                    //基本演示
                    tree.render({
                        elem: '#test12'
                        , data: res
                        , showCheckbox: true  //是否显示复选框
                        , id: 'demoId1'
                        // , isJump: true //是否允许点击节点时弹出新窗口跳转
                        , click: function (obj) {
                            var data = obj.data;  //获取当前点击的节点数据
                            // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                        }
                    });
                }
            });

            var jsonData = JSON.stringify(data2); // 将roleArray转换为JSON字符串
            $.ajax({
                url: 'http://localhost:8080/user_role/add',
                data: jsonData,
                contentType: 'application/json', // 设置内容类型为JSON
                type: 'post',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('token', localStorage.getItem("token"));
                },
            })



            form.on('submit(user-save)', function (data) {
                let roleIds = "";
                $('input[type=checkbox]:checked').each(function () {
                    roleIds += $(this).val() + ",";
                });
                roleIds = roleIds.substr(0, roleIds.length - 1);
                data.field.roleIds = roleIds;


                $.ajax({
                    url: 'http://localhost:8080/role/update',
                    data: data.field,
                    type: 'post',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('token', localStorage.getItem("token"));
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 1000 });
                        }
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>